﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace MMOConnector
{
    public partial class ConnectForm : Form
    {
        private TcpClient _client;
        private NetworkStream _stream;
        private BinaryFormatter _bf;

        public ConnectForm()
        {
            InitializeComponent();

            ConnectToMasterServer();
        }

        ~ConnectForm()
        {
            _client.Close();
        }

        private void ConnectToMasterServer()
        {
            try
            {
                _client = new TcpClient("127.0.0.1", 9876); //new TcpClient("spokeno.ath.cx",9876);
                _stream = _client.GetStream();
                _bf = new BinaryFormatter();
            } catch(SocketException e){
                MessageBox.Show("Connection to master server failed. Server may be overloaded currently," +
                    " please try again in a short while. If this error continues to occur, please contact" +
                    " technical support. Error: " + e.Message);
                this.masterStatusL.ForeColor = Color.Red;
                this.masterStatusL.Text = "Connection to master server failed.";
                this.connectB.Enabled = false;
                return;
            }
            this.masterStatusL.ForeColor = Color.Black;
            this.masterStatusL.Text = "Connected to master server. Server is waiting for login details";
            this.connectB.Enabled = true;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void connectB_Click(object sender, EventArgs e)
        {
            if (_client.Connected)
            {
                string user = this.usernameT.Text;
                string pass = this.passwordT.Text;
                MMOShared.LoginDetails ld = new MMOShared.LoginDetails(user, pass);
                _bf.Serialize(_stream, ld);
                _stream.Flush();
                while (true)
                {
                    if (_stream.DataAvailable)
                    {
                        int b = _stream.ReadByte();
                        if (b == 0)
                        {
                            this.masterStatusL.ForeColor = Color.Red;
                            this.masterStatusL.Text = "Authentication failed! Username and password did not match up.";
                            _client.Close();
                            ConnectToMasterServer();
                            return;
                        }
                        else if (b == 1)
                        {
                            serverListForm slf = new serverListForm(_client);
                            slf.ShowDialog();
                            this.Close();
                            return;
                        }
                    }
                }
            }
        }

        private void cancelB_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
